<template>
  <j-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    :ok-button-props="{ props: { disabled: disableSubmit } }"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <a-form :form="form">
        <a-row>
          <a-col :span="12">
            <a-form-item label="员工姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="true"
                v-decorator="['name', validatorRules.name]"
                placeholder="请输入员工姓名"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="月薪总额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="true"
                v-decorator="['wagesAmount', validatorRules.wagesAmount]"
                placeholder="请输入月薪总额"
              ></a-input>
            </a-form-item>
          </a-col>
          <!-- <a-col :span="12">
            <a-form-item label="所属部门" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-category-select
                :disabled="true"
                v-decorator="['departmentId', validatorRules.departmentId]"
                pcode="{table:'HrDepartment',txt:'name',key:'departmentId',idField:'id',pidField:'parentId',hasChildField:'hasChild', condition:' parentId = 0'}"
                placeholder="请选择所属部门"
                back="title"
              />
            </a-form-item>
          </a-col> -->
        
        </a-row>
      </a-form>

      <!-- 子表单区域 -->
      <a-tabs v-model="activeKey" @change="handleChangeTabs">
        <a-tab-pane tab="薪资项表" :key="refKeys[0]" :forceRender="true">
          <j-editable-table
            :disabled="disableSubmit"
            :ref="refKeys[0]"
            :tableName="refKeys[0]"
            :loading="HrSalaryTable.loading"
            :columns="HrSalaryTable.columns"
            :dataSource="HrSalaryTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="!disableSubmit"
          />
        </a-tab-pane>
      </a-tabs>
    </a-spin>
  </j-modal>
</template>

<script>
import pick from 'lodash.pick'
import { FormTypes, getRefPromise } from '@/utils/JEditableTableUtil'
import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
import { validateDuplicateValue } from '@/utils/util'
import JUpload from '@/components/jeecg/JUpload'
import JDictSelectTag from '@/components/dict/JDictSelectTag'
import JSearchSelectTag from '@/components/dict/JSearchSelectTag'
import JCategorySelect from '@/components/jeecg/JCategorySelect'
import { getAction } from '@/api/manage'

export default {
  name: 'ClmEmployeeModal',
  mixins: [JEditableTableMixin],
  components: {
    JUpload,
    JDictSelectTag,
    JSearchSelectTag,
    JCategorySelect
  },
  data() {
    return {
      disableSubmit: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      labelCol2: {
        xs: { span: 24 },
        sm: { span: 3 },
      },
      wrapperCol2: {
        xs: { span: 24 },
        sm: { span: 20 },
      },
      // 新增时子表默认添加几行空数据
      addDefaultRowNum: 1,
      validatorRules: {
        wagesAmount: {
          rules: [],
        },
        name: {
          rules: [{ required: true, message: '请输入员工姓名!' }],
        },
      },
      refKeys: ['HrSalaryTable'],
      tableKeys: ['HrSalaryTable'],
      activeKey: 'HrSalaryTable',
      // 薪资项表
      HrSalaryTable: {
        loading: false,
        dataSource: [],
        columns: [
          {
            title: '薪资项',
            key: 'salaryTypeId',
            type: FormTypes.select,
            dictCode: 'DataDictionary,description,dictionaryId, dictionaryTypeId = SALARY_TYPE',
            width: '200px',
            placeholder: '请选择${title}',
            defaultValue: '',
            validateRules: [{ required: true, message: '${title}不能为空' }],
          },
          {
            title: '金额(元)',
            key: 'amount',
            type: FormTypes.input,
            width: '200px',
            placeholder: '请输入${title}',
            defaultValue: '',
            validateRules: [{ required: true, message: '${title}不能为空' }],
          },
          {
            title: '开始日期',
            key: 'fromDate',
            type: FormTypes.date,
            width: '200px',
            placeholder: '请选择${title}',
            defaultValue: '',
            dataIndex: (text) => {
              return moment(text).format('YYYY-MM-DD')
            },
            validateRules: [{ required: true, message: '${title}不能为空' }],
          },
          {
            title: '截止日期',
            key: 'thruDate',
            type: FormTypes.date,
            width: '200px',
            placeholder: '请选择${title}',
            dataIndex: (text) => {
              return moment(text).format('YYYY-MM-DD')
            },
            defaultValue: '',
          },
        ],
      },
      url: {
        add: '/online/special/api/savePersonHrSalary',
        edit: '/online/special/api/savePersonHrSalary',
        list: '/online/api/listHrSalary',
      },
    }
  },
  methods: {
    getAllTable() {
      let values = this.tableKeys.map((key) => getRefPromise(this, key))
      return Promise.all(values)
    },
    /** 调用完edit()方法之后会自动调用此方法 */
    editAfter() {
      let fieldval = pick(this.model, 'name', 'wagesAmount','departmentId')
      this.$nextTick(() => {
        this.form.setFieldsValue(fieldval)
      })
      // 加载子表数据
      if (this.model.partyId) {
        let params = { partyId: this.model.partyId }
        this.requestTableData(this.url.list, params, this.HrSalaryTable)
      }
    },
    requestTableData(url, params, tab, success) {
      tab.loading = true
      getAction(url, params).then(res => {
        tab.dataSource = res.result.records || [];
        typeof success === 'function' ? success(res) : ''
      }).finally(() => {
        tab.loading = false
      })
    },
    /** 整理成formData */
    classifyIntoFormData(allValues) {
      let main = Object.assign(this.model, allValues.formValue)
      let tableInfo = allValues.tablesValue[0].values.map((item) => {
        return item.partyId = main.partyId
      })
      return {
        // ...main, // 展开
        hrSalaryList: allValues.tablesValue[0].values
      }
      console.log(allValues)
    },
    validateError(msg) {
      if (msg) {
        this.isHandleOk = false
      }
      this.$message.error(msg)
    },
    popupCallback(row) {
      this.form.setFieldsValue(pick(row, 'name', 'wagesAmount','departmentId'))
    },
  },
}
</script>

<style scoped>
</style>
